package other.xsy.pms;

import com.alibaba.fastjson.JSONArray;
import com.rkhd.platform.sdk.data.model.Opportunity;
import com.rkhd.platform.sdk.exception.ScriptBusinessException;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.model.XObject;
import com.rkhd.platform.sdk.trigger.DataResult;
import com.rkhd.platform.sdk.trigger.Trigger;
import com.rkhd.platform.sdk.trigger.TriggerRequest;
import com.rkhd.platform.sdk.trigger.TriggerResponse;
import other.xsy.pms.utils.PromotionApiUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 转移业务机会推送PMS 考虑批量转移的情况
 * @Author 黎炎松
 * 2024-09-23 13:56
 */
public class TransferOpportunityPush implements Trigger {

    private static final Logger logger = LoggerFactory.getLogger();
    private static String LOGGER_MSG = "转移业务机会推送PMS ==> ";

    @Override
    public TriggerResponse execute(TriggerRequest request) throws ScriptBusinessException {
        logger.debug(LOGGER_MSG + " 触发开始 ");
        List<XObject> dataList = request.getDataList();
        List<DataResult> dataResultList = new ArrayList<>();
        for (XObject xObject : dataList) {
            Opportunity opportunity = (Opportunity) xObject;
            dataResultList.add(transferPush(opportunity));
        }

        return new TriggerResponse(true, "处理完成", dataResultList);
    }

    /**
     * 转移推送
     * @param opportunity
     */
    public DataResult transferPush(Opportunity opportunity){
        SyncOpportunityToPmsTrigger syncOpt = new SyncOpportunityToPmsTrigger();
        syncOpt.executeSync(opportunity.getId());
        logger.info(LOGGER_MSG + " 处理结束");
        return new DataResult(true,"校验完成" , opportunity);
    }

}
